package com.dlc.felear.xiaoensale.dao;

import android.content.Context;
import android.util.Log;

import com.dlc.felear.xiaoensale.entity.ActionStepItem;
import com.dlc.felear.xiaoensale.helper.JsonHelper;
import com.j256.ormlite.dao.Dao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class ActionStepItemDao {
    private static final String TAG = "ActionStepItemDao";
    private Dao<ActionStepItem, Integer> PipeInfoDao;

    /**
     * 构造方法
     * 获得数据库帮助类实例，通过传入Class对象得到相应的Dao
     *
     * @param context
     */
    public ActionStepItemDao(Context context) {
        try {
            PipeInfoDao = DetailDataOpenHelper.getInstance(context).getDao(ActionStepItem.class);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * 添加一条记录
     *
     * @param PipeInfo
     */
    public void add(ActionStepItem PipeInfo) {
        try {
            if (queryForAllByName(PipeInfo.getName()) == null) {
                PipeInfoDao.create(PipeInfo);
            } else {
                Log.e(TAG, "已添加：" + JsonHelper.toJson(queryForAllByName(PipeInfo.getName())));
            }
        } catch (SQLException e) {
            e.printStackTrace();
            Log.e(TAG, e.getMessage());
        }
    }

    public void addAll(List<ActionStepItem> list) {
        if (list == null) {
            return;
        }
        for (ActionStepItem actionStepItem : list) {
            add(actionStepItem);
        }
    }

    /**
     * 删除一条记录
     *
     * @param PipeInfo
     */
    public void delete(ActionStepItem PipeInfo) {
        try {
            PipeInfoDao.delete(PipeInfo);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * 删除一条记录
     *
     * @param list
     */
    public void delete(List<ActionStepItem> list) {
        try {
            for (ActionStepItem actionStepItem : list) {
                delete(actionStepItem);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /**
     * 删除一条记录
     *
     */
    public void deleteAll() {
        try {
            delete(queryForAll());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 查询一条记录
     *
     * @param id
     * @return
     */
    public ActionStepItem queryForId(int id) {
        ActionStepItem PipeInfo = null;
        try {
            PipeInfo = PipeInfoDao.queryForId(id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return PipeInfo;
    }


    /**
     * 查询所有记录
     *
     * @return
     */
    public List<ActionStepItem> queryForAll() {
        List<ActionStepItem> PipeInfos = new ArrayList();
        try {
            PipeInfos = PipeInfoDao.queryForAll();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return PipeInfos;
    }

    /**
     * 查询所有记录
     *
     * @return
     */
    public List<ActionStepItem> queryForAllByAction(int type) {
        List<ActionStepItem> PipeInfos = new ArrayList();
        try {
            PipeInfos = PipeInfoDao.queryBuilder().where().eq("type", type).query();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return PipeInfos;
    }

    /**
     * 查询所有记录
     *
     * @return
     */
    public ActionStepItem queryForAllByName(String name) {
        ActionStepItem actionStepItem = null;
        List<ActionStepItem> PipeInfos = new ArrayList();
        try {
            PipeInfos = PipeInfoDao.queryBuilder().where().eq("name", name).query();
            if (PipeInfos != null && !PipeInfos.isEmpty()) {
                actionStepItem = PipeInfos.get(0);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return actionStepItem;
    }

}
